package com.teamcalendar.client.controller;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import com.teamcalendar.client.DBAdapter;
import com.teamcalendar.client.InteractiveArrayAdapter;
import com.teamcalendar.client.Member;
import com.teamcalendar.client.MemberList;

public class ForwardActivity extends ListActivity {

	DBAdapter mDb = new DBAdapter(this);
	Bundle bundle;
	String memberId,senderId;
	List<Member> list;
	MemberList selectedMembers=new MemberList();
	MemberList members;
	Button mButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forword_message);
		mButton=(Button) findViewById(R.id.okforword_button);

		mDb.open();
		bundle=getIntent().getExtras();
		memberId=bundle.getString("memberId");
		senderId=bundle.getString("senderId");
		selectedMembers=bundle.getParcelable("selectedMembers");
		listAllRecipients();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDb.close();
	};

	private void listAllRecipients() {
		list			=	new ArrayList<Member>();
		Cursor mCursor	=	mDb.getAllMyTeamMembers();
		mCursor.moveToFirst();
		String firstName,lastName,id;

		while(mCursor.isAfterLast()==false){
			id	=	mCursor.getString(mCursor.getColumnIndex(
					DBAdapter.KEY_MEMBERID));
			firstName	=	mCursor.getString(mCursor.getColumnIndex(
					DBAdapter.KEY_FIRSTNAME));
			lastName	=	mCursor.getString(mCursor.getColumnIndex(
					DBAdapter.KEY_LASTNAME));
			Member m=	new Member(firstName,lastName);
			m.setId(id);
			if(id.equals(memberId) || id.equals(senderId)){
				mCursor.moveToNext();
			}else{
				list.add(m);
				mCursor.moveToNext();
			}

		}
		mCursor.close();

		if(selectedMembers.size()>0){
			for (int i=0;i<list.size();i++){
				System.out.println("list " + list.get(i).getId());

				for(Member selectedMember:selectedMembers){

					if(list.get(i).getId().equals(selectedMember.getId())){
						System.out.println("selected " + selectedMember.getId());

						list.get(i).setSelected(true);
					}


				}
				
			}
		}
		// Create an array of Strings, that will be put to our ListActivity
		ArrayAdapter<Member> adapter = new InteractiveArrayAdapter(this,
				list,mButton);
		setListAdapter(adapter);
	}

	public void okButton(View v){
		members	=	new MemberList();
		for(Member member:list){
			if(member.isSelected()){
				members.add(member);
			}
		}
		System.out.println("selected " + members.size());
		Bundle b = new Bundle();
		b.putParcelable("members", members);
		Intent recipient = new Intent();
		recipient.putExtras(b);
		recipient.putExtra("memeberId", memberId);
		recipient.putExtra("senderId", senderId);
		setResult(RESULT_OK, recipient);
		finish();
	}
	
	public void cancelButton(View v){
		setResult(RESULT_CANCELED,null);
		finish();
	}
}
